package cn.simon.free.redis.jedis.list;

import cn.simon.free.redis.jedis.common.AbstractJedis;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * redis - list
 *
 * @author simon
 * @date 2021/5/18 14:43
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Component
public class JedisListSupport extends AbstractJedis implements JedisList {

    @Override
    public Long lPush(String key, String value) {
        return super.redisTemplate.opsForList().leftPush(key, value);
    }

    @Override
    public Long rPush(String key, String value) {
        return super.redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public Object lPop(String key) {
        return super.redisTemplate.opsForList().leftPop(key);
    }

    @Override
    public Object rPop(String key) {
        return super.redisTemplate.opsForList().rightPop(key);
    }

    @Override
    public Object bLPop(String key, long timeOut) {
        return super.redisTemplate.opsForList().leftPop(key, timeOut, TimeUnit.SECONDS);
    }

    @Override
    public Object bRPop(String key, long timeOut) {
        return super.redisTemplate.opsForList().rightPop(key, timeOut, TimeUnit.SECONDS);
    }

    @Override
    public Long lPushX(String key, String value) {
        return super.redisTemplate.opsForList().leftPushIfPresent(key, value);
    }

    @Override
    public Long rPushX(String key, String value) {
        return super.redisTemplate.opsForList().rightPushIfPresent(key, value);
    }

    @Override
    public List<Object> lRange(String key, int start, int stop) {
        return super.redisTemplate.opsForList().range(key, start, stop).stream().map(serializable -> String.valueOf(serializable)).collect(Collectors.toList());
    }

    @Override
    public Long lRem(String key, int count, Object value) {
        return super.redisTemplate.opsForList().remove(key, count, value);
    }

    @Override
    public String rPopLPush(String source, String destination) {
        return super.redisTemplate.opsForList().rightPopAndLeftPush(source, destination);
    }

    @Override
    public Object bRPopLPush(String source, String destination, long timeOut) {
        return super.redisTemplate.opsForList().rightPopAndLeftPush(source, destination, timeOut, TimeUnit.SECONDS);
    }

    public Long lPush(String key, String... values) {
        return super.redisTemplate.opsForList().leftPushAll(key, values);
    }

    public Long rPush(String key, String... values) {
        return super.redisTemplate.opsForList().rightPushAll(key, values);
    }
}
